package com.order.model;  

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jetty.util.security.Credential.MD5;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.order.shiro.ShiroUtils;
import com.order.utils.DateUtils;
import com.order.utils.JsonUtils;

/** 
 * @Title: Account.java 
 * @Package com.order.model 
 * @Description: 账户类
 * @author LinBo
 * @date 2014-3-25 下午11:05:02 
 */

@TableBind(tableName="account", pkName="id")
public class Account extends Model<Account> {

	/** 
	 * @Fields serialVersionUID : 可序列话版本
	 */  
	private static final long serialVersionUID = 8642282792966065388L;
	public static final Account dao = new Account();
	public static final String ID = "id";
	public static final String NAME = "name";
	public static final String PASSWORD = "password";
	public static final String GENDER = "gender";
	public static final String ADDRESS = "address";
	public static final String MAIL = "mail";
	public static final String QQ = "qq";
	public static final String IDENTIFY = "identify";
	public static final String VIP = "vip";
	public static final String STATUS = "status";
	public static final String CREATE_DATE = "create_date";
	public static final String LAST_UPDATED_BY = "last_updated_by";
	public static final String LAST_UPDATE_DATE = "last_update_date";
	public static final String PHONE = "phone";
	public Collection<String> gePpermissionsNameList() {
		// TODO Auto-generated method stub
		return null;
	}

	public Map<String, Object> getAccountJson(int page, int pagesize,JSONObject jsonObj) {
		String select = "select a.id, a.name, a.gender, a.address, a.mail, a.qq, "
				+ " a.status, a.vip, a.identify, a.create_date  ";
		String sqlExceptSelect = "from account a where 1 = 1 ";

		System.out.println(jsonObj);
		// 用户名
		if (StringKit.notBlank(jsonObj.getString("NAME"))) {
			String name = jsonObj.getString("NAME");
			sqlExceptSelect += "  and a.name like '%" + name + "%'";
		}
		// 状态
		if (StringKit.notBlank(jsonObj.getString("STATUS"))) {
			String email = jsonObj.getString("STATUS");
			sqlExceptSelect += "  and a.status ='" + email + "'";
		}
		
		// 性别
		if (StringKit.notBlank(jsonObj.getString("GENDER"))) {
			String gender = jsonObj.getString("GENDER");
			sqlExceptSelect += "  and a.gender ='" + gender + "'";
		}

		// VIP
		if (StringKit.notBlank(jsonObj.getString("VIP"))) {
			String vip = jsonObj.getString("VIP");
			sqlExceptSelect += "  and a.vip ='" + vip + "'";
		}
		
		// 创建日期
		if (StringKit.notBlank(jsonObj.getString("CREATEDATE"))) {
			String createDate = jsonObj.getString("CREATEDATE");
			sqlExceptSelect += "  and a.create_date like '%" + createDate + "%'";
		}
		
		// 住址
		if (StringKit.notBlank(jsonObj.getString("ADDRESS"))) {
			String address = jsonObj.getString("ADDRESS");
			sqlExceptSelect += "  and a.address like '%" + address + "%'";
		}
		Page<Account> list = dao.paginate(page, pagesize, select,sqlExceptSelect);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("Rows", list.getList());
		map.put("Total", list.getTotalRow());
		return map;
	}
	
	public Account getObj(String json) {
		try {
			return setAttrs(JsonUtils.fastjson2Map(json));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return this;
	}
	
	/**保存*/
	public boolean mySave() {
		String password = MD5.digest(getStr(PASSWORD));
		set(ID, null);
		set(NAME, getStr(NAME));
		set(PASSWORD, password);
		set(GENDER, get(GENDER));
		set(ADDRESS, get(ADDRESS));
		set(MAIL, get(MAIL));
		set(QQ, get(QQ));
		set(IDENTIFY, get(IDENTIFY));
		set(VIP, get(VIP));
		set(STATUS, get(STATUS));
		set(CREATE_DATE, get(CREATE_DATE));
		set(LAST_UPDATED_BY, ShiroUtils.getUserId());
		set(LAST_UPDATE_DATE, DateUtils.nowDate());
		return save();
	}
	
	/**更新*/
	public boolean myUpdate() {
		set(ID, get(ID));
		set(VIP, get(VIP));
		set(STATUS, get(STATUS));
		set(LAST_UPDATED_BY, ShiroUtils.getUserId());
		set(LAST_UPDATE_DATE, DateUtils.getNowTime());
		return update();
	}
	
	// 更新密码
	public boolean password(String newpassword, int userid) {
		return dao.findById(userid).set("password", MD5.digest(newpassword)).update();
	}
	
	// 验证旧密码
	public boolean validatorpwd(String password, int userid) {
		return dao.findFirst("select 1 from account where id = ? and password = ?",userid, MD5.digest(password)) != null;
	}

	/** 
	* @Title: isAccountEnabled 
	* @Description: 判断是否启用
	* @param accountName
	* @return   
	*/ 
	public boolean isAccountEnabled(String accountName) {
		String sql = "select count(1) from account a where a.status='1' and a.name=?";
		int num = Db.queryNumber(sql, accountName).intValue();
		if(num == 0){
			return false;
		}
		return true;
	}

	/** 
	* @Title: checkAcount 
	* @Description: 验证用户名和密码
	* @param accountName
	* @param password2
	* @return   
	*/ 
	public boolean checkAcount(String accountName, String password) {
		String sql = "select count(1) from account a where a.name=? and a.password=? ";
		int num = Db.queryNumber(sql, accountName, password).intValue();
		if(num == 0){
			return false;
		}
		return true;
	}

	/** 
	* @Title: getAcount 
	* @Description: 获取用户
	* @param account
	* @return   
	*/ 
	public Account getAcount(String name, String password) {
		String sql = "select * from account a where a.name=? and a.password=? ";
		return dao.findFirst(sql, name, password);
	}

	/** 
	* @Title: isNameEnable 
	* @Description: 判断客户名是否可用（不重复）
	* @param name2
	* @return   
	*/ 
	public boolean isNameEnable(String name) {
		String sql = "select count(1) from account where name = ? ";
		int num = Db.queryNumber(sql, name).intValue();
		if(num == 0){
			return true;
		}else{
			return false;
		}
	}
}
  